<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('fixedAssets.fixedAssets')"
				:rightText="$t('distributedEnter.scanRecord')" @clickRight="clickRight"></nav-bar-custom>
		</template>
		<template v-slot:gBody>

			<!--最上面的搜索内容-->
			<view class="distributed">
				<!-- 库存信息-->
				<view class="custom-form-card">
					<view class="custom-card-title">
						<text class="title-line"></text>
						{{ $t('multilingual.inventoryInformation') }}
					</view>
					<view class="custom-card-itemOne">
						<text class="item-info-title">{{ $t('finishedInventory.inventoryNumber') }}：</text>
						{{ data.billCodeInfo.billCode }}
					</view>
					<view class="custom-card-itemOne addflex">
						<text class="item-info-title itemflex">{{ $t('fixedAssets.assetCardNo') }}：<text
								style="color: #333;">{{ data.billCodeInfo.cardCount }}</text></text>
						<text class="item-info-title itemflex">{{ $t('fixedAssets.assetTotal') }}：<text
								style="color: #333;">{{ data.billCodeInfo.cardSumQty }}</text></text>
					</view>
					<view class="custom-card-itemOne addflex">
						<text class="item-info-title itemflex">{{ $t('JsInternational.ofStocktaked') }}：<text
								style="color: #333;">{{ data.billCodeInfo.checkQty }}</text></text>
						<text class="item-info-title itemflex">{{ $t('JsInternational.notStocktaking') }}：<text
								style="color: #333;">{{ data.billCodeInfo.noCheckQty }}</text></text>
					</view>
				</view>
				<!-- 扫描条码 -->
				<view class="custom-form-card">
					<view class="custom-card-title">
						<text class="title-line"></text>
						{{ $t('distributedEnter.barcodeScan') }}
					</view>
					<view class="custom-card-scan">
						<uni-easyinput class="uni-mt-5" suffixIcon="scan" v-model="data.scanValue"
							:placeholder="$t('stockManage.pleaseScanCode')" @confirm="confirm" @iconClick="iconClick">
						</uni-easyinput>
					</view>
				</view>
				<view class="gui-flex gui-rows gui-nowrap gui-align-items-center gui-margin-top">
					<view class="gui-title-line"></view>
					<text class="gui-primary-text gui-h6" style="padding-left:50rpx; padding-right:50rpx;">{{
						$t('fixedAssets.cardInfor') }}</text>
					<view class="gui-title-line"></view>
				</view>
				<!-- 底部显示区域 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolower">
					<!-- 当前扫描条码 -->
					<view v-if="data.scanInfo" class="custom-form-card">
						<uni-collapse v-model="data.value">
							<uni-collapse-item titleBorder="true" :show-animation="true">
								<template v-slot:title>
									<view class="custom-card-itemOne" style="height:80rpx">
										<text class="flex-1">
											<text class="item-info-title">{{ $t('fixedAssets.cardNo')
												}}：</text>
											{{ data.scanInfo.cardCode }}
											<uni-tag style="margin-left: 20rpx;" :mark="true"
												:text="$t('finishInventory.checked')" type="primary" size="default"
												v-if="data.scanInfo.ckeckFlag == 'Y'" />
											<uni-tag style="margin-left: 20rpx;" :mark="true"
												:text="$t('finishInventory.unchecked')" size="default" v-else />
										</text>
									</view>
								</template>
								<view class="content">
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('fixedAssets.assetType') }}：</text>{{
											data.scanInfo.assetClass }}
									</view>
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('fixedAssets.assetName') }}：</text>{{
											data.scanInfo.assetName }}
									</view>
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('fixedAssets.purchaseDate') }}：</text>{{
											data.scanInfo.specification }}
									</view>
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('fixedAssets.warrantyPeriod') }}：</text>{{
											data.scanInfo.assetsNumber }}
									</view>
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('distributedEnter.uom') }}：</text>{{
											data.scanInfo.unit }}
									</view>
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('fixedAssets.assetNo') }}：</text>{{
											data.scanInfo.assetCode }}
									</view>
									<view class="custom-card-itemOne">
										<text class="item-info-title">{{ $t('fixedAssets.startUse') }}：</text>{{
											data.scanInfo.startDate }}
									</view>
									<view class="divider"></view>
									<view class="custom-card-itemOne">
										<view style="margin-left: 73%;margin-bottom:20rpx;">
											<view class="btnCancel" v-if="data.scanInfo.ckeckFlag == 'Y'"
												@click="HandUnCheckPda(data.scanInfo)">
												{{ $t('finishInventory.cancel') }}
											</view>
											<view class="btnPanDian" v-else @click="assetsCheck(data.scanInfo)">
												{{ $t('finishInventory.check') }} </view>
										</view>
									</view>
								</view>
							</uni-collapse-item>
						</uni-collapse>
					</view>
					<view style="height:100rpx"></view>
				</scroll-view>
				<view class="page-bottom-boxtwo">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="handelCheck">
						<!-- {{ $t('pageTranslation.approve') }} -->
						{{ data.buttonText }}
					</view>
				</view>
			</view>
		</template>
	</gui-page>
</template>


<script setup>
import { navigate, navigateBack } from '@/utils/routerSkip.js';
import { ref, reactive, toRaw, watch, computed, onMounted } from 'vue';
import storageBin from '@/components/storageBin.vue';
import comboGrid from "@/components/comboGrid";
import { onShow, onHide} from '@dcloudio/uni-app';

import { TEST_URL, PROD_URL, IMG_BASE_URL } from '@/constant/config.js'
import { TOKEN } from '@/constant/index.js';
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_COMMIT, OPERATION_SCAN, OPERATION_LOOK, USERINFO } from '@/constant/index.js'
import { getDataDictionaryFn, base64toFile } from '@/utils/index.js'
//API引入
import { getAssetCardCheckStockInfo_PDA, scanAssetCard_PDA, checkAssetCardOrNO_PDA, auditAssetCardCheckStock_PDA } from '@/api/api.js'
//扫描条码的引入
import pdaScan from '@/hooks/pdaScan.js'
import { useStore } from "vuex";
import { forEach } from '../../uni_modules/u-ajax/js_sdk/lib/utils';

//国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const { scan } = pdaScan()
let store = useStore();

//注册组件
components: {
	storageBin,
		comboGrid
}

// 接受来自父组件的传参 页面传值
const props = defineProps({
	id: String
})

let data = reactive({
	billCodeInfo: [], // 最上面的库存汇总信息
	id: props.id,// 上级界面传过来的id
	scanInfo: undefined,
	formData: '',// 签名信息
	scanValue: '',
	value: ['0'],
	list: [],
	scanInfo: undefined,// 请求返回盘点数据
	noticeHooks: false, //暂无相关数据标识
	userInfo: uni.getStorageSync(USERINFO) || {},
	buttonText: t('finishInventory.signature'),
})

// 签名信息
data.formData = computed(() => {
	return store.state.fromDataAssets
})

onShow(() => {
	const pages = getCurrentPages();
	if (pages[pages.length - 1].route === '/pages/fixedAssets/fixedAssets') {
		uni.$on('scancodedate', (data) => {
		console.log('获取到的code：' + data.code)
		getCode(data.code, 'pda')
		})
	}
	getRequestData()
})

onHide(()=>{
	uni.$off("scancodedate")
})

onMounted(() => {
	// console.log('开始监听PDA扫码')
	// uni.$on('scancodedate', (data) => {
	// 	console.log('获取到的code：' + data.code)
	// 	getCode(data.code, 'pda')
	// })
	// getRequestData()
})

// 通過盘点单号id查询汇总信息
const getRequestData = () => {
	let parmes = {
		headerId: data.id
	}
	getAssetCardCheckStockInfo_PDA(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '固资盘点'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.msg,//错误信息
				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_LOOK,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.code == 0) {
			data.billCodeInfo = res.data
			// watcShowscanList() // 去手动调取计算监属性
		}
	})
}

// 用户调起相机扫码
const iconClick = async (type) => {
	if (type === 'suffix') {
		let code = await scan()
		getCode(code, 'scan')
	}
}
// 用户点击键盘上搜索
const confirm = () => {
	getCode(data.scanValue, 'keydown')
}
// 获取code
const getCode = (code, type) => {
	data.scanValue = code
	let parmes = {
		headerId: data.id,
		cardCode: code
	}
	scanAssetCard_PDA(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '盘点'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.msg,//错误信息
				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_SCAN,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.code == 0) {
			data.scanInfo = res.data
		} else {
			data.scanInfo=undefined
			uni.showToast({ title: res.msg, icon: 'none' })
		}
	})
}

// 取消盘点
const HandUnCheckPda = (item) => {
	let parmes = {
		detailId: item.checkDId,
		CheckQty: 0,
		CheckUserName: data.userInfo.name,
		CheckUserNo: data.userInfo.username,
		isCheck: 'N'
	}
	chectItem(parmes)
}
// 盘点
const assetsCheck = (item) => {
	let parmes = {
		detailId: item.checkDId,
		CheckQty: item.assetsNumber,
		CheckUserName: data.userInfo.name,
		CheckUserNo: data.userInfo.username,
		isCheck: 'Y'
	}
	chectItem(parmes)
}
const chectItem = (parmes) => {
	uni.showLoading({
		title: 'loading',
		mask: true
	});
	checkAssetCardOrNO_PDA(parmes).then(res => {
		if (res.code == 0) {
			//隐藏加载框
			uni.hideLoading();
			uni.showToast({
				title: t('smashRemoval.submitSuccessfully'),
				icon: 'none',
				complete: () => {
					getRequestData() //重新执行查询
					getCode(data.scanInfo.cardCode)
				}
			})
		} else {
			uni.showToast({
				title: res.msg,
				icon: "none"
			});
		}
	})
}
// 查看记录
const clickRight = () => {
	navigate({
		url: `/pages/fixedAssets/fixedAssetsRecord??billCode=${data.billCodeInfo.billCode}&billid=${data.id}`
	})
}

// 审核
const handelCheck=async()=>{
	//判断用户是否有提交权限
	let bool = await store.dispatch('checkUser')
	if (!bool) {
		uni.showToast({
			title: t('JsInternational.sorryAuthority'),
			icon: 'none'
		})
		return
	}

	//打开签名页面
	if (!data.formData.length) {
		navigate({
			url: `/pages/fixedAssets/signature?id=${data.id}&billCode=${data.billCodeInfo.billCode}`
		})
		return
	}
}

// 审核调用接口
// const checkRequest= () => {
// 	let parmes = {
// 		detailId: data.id,
// 		billCode: data.billCodeInfo.billCode,
// 		CheckUserName: data.userInfo.name,
// 		CheckUserNo: data.userInfo.username,
// 	}
// 	auditAssetCardCheckStock_PDA(parmes).then(res => {
// 		try {
// 			let inputParameter = JSON.stringify(parmes)
// 			let returnParameter = JSON.stringify(res)
// 			let operationPicture = '固资盘点'
// 			let callInterface = res.rquestUrl
// 			let opertionParmes = {
// 				inputParameter,
// 				returnParameter,
// 				operationPicture,
// 				callInterface,
// 				responseCode: res.code,//返回状态
// 				errorMessage: res.msg,//错误信息
// 				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
// 				operationType: OPERATION_COMMIT,
// 				module: 'WMS',
// 				source: "PDA",
// 			}
// 			basPdaOperationLogFn(opertionParmes)
// 		} catch (error) {
// 		}
// 		if (res.code == 0) {
// 			uni.showToast({
// 				title: t('smashRemoval.submitSuccessfully'),
// 				icon: 'none',
// 				complete: () => {
//                     store.commit('delFormDataAssets')
// 					uni.navigateBack({
// 						delta: 1
// 					});
// 				}
// 			})
// 		} else {
// 			uni.showToast({
// 				title: res.msg,
// 				icon: "none"
// 			});
// 		}
// 	})
// }
</script>

<style lang="scss" scoped>
// .card-my{
// 	display: flex;
// 	flex-direction: column;
// }
.addflex {
	display: flex;
	align-items: center;

	.itemflex {
		flex: 1;
	}
}

.distributed {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 200rpx);
	// height: 100vh;
}

.scroll {
	margin-top: 15rpx;
	flex: 1;
	height: 2rpx;
}

.page-bottom-box {
	position: fixed;
}

.addborder {
	border: 1rpx solid #DCDFE6;
	box-sizing: border-box;
	padding: 15rpx;
	margin-bottom: 15rpx;
	border-radius: 8rpx;
}

.input-dialog-itembox {
	padding: 15rpx;
	box-sizing: border-box;
	border: 1rpx solid #DCDFE6;
	margin-bottom: 15rpx;
	border-radius: 8rpx;
}

.input-dialog-item {
	font-size: 24rpx;
}

.input,
.textarea {
	font-size: 24rpx;
}

.iconDelete {
	margin-top: 20rpx;
	height: 100%;
	width: 40rpx;
}

.item-info-title {
	margin-left: 10px;
}


.btnSuccess {
	width: 120rpx;
	height: 40rpx;
	line-height: 40rpx;
	color: #fff;
	background-color: #007aff;
	display: inline-block;
	text-align: center;
	border-radius: 10rpx;
}

.btnError {
	width: 120rpx;
	height: 40rpx;
	line-height: 40rpx;
	color: #fff;
	background-color: red;
	display: inline-block;
	text-align: center;
	border-radius: 10rpx;
}


.btnCancel {
	width: 120rpx;
	height: 40rpx;
	line-height: 40rpx;
	color: #fff;
	background-color: #f08d49;
	display: inline-block;
	text-align: center;
	border-radius: 10rpx;
}

.btnPanDian {
	width: 120rpx;
	height: 40rpx;
	line-height: 40rpx;
	color: #fff;
	background-color: #42b983;
	display: inline-block;
	text-align: center;
	border-radius: 10rpx;
}

.flexRight {

	display: flex;
	flex-direction: row;
	justify-content: flex-end;
}
</style>
